// ################################################# javascript jqplot graphics ###########################################
function createDashboadChart(psoValues,clonalValues) {
	var psoTimeList = getJavaArrayAsAString(psoValues);
	var clonalTimeList = getJavaArrayAsAString(clonalValues);	  
	  $(document).ready(function(){
		  var plot2 = $.jqplot ('dashboardchart', [psoTimeList,clonalTimeList], {

    		  title: 'Recomandation particle/time evaluation',
    	      axesDefaults: {
    	        labelRenderer: $.jqplot.CanvasAxisLabelRenderer
    	      },
    	      seriesDefaults: {
    	          rendererOptions: {
    	              smooth: true
    	          }
    	      },
    	      axes: {
    	        xaxis: {
    	          label: "Nr. of particles * 10",
    	          min : 1,
    	          max : 10,
    	          numberTicks : 10
    	        },
    	        yaxis: {
    	          label: "Time * 1000 ms",
    	          min :5,
    	          max : 65
    	        }
    	      },
    	      highlighter: {
    	    	  showTooltip : true,
    	    	  tooltipLocation : "nw",
    	    	  fadeTooltip : true
    	      },
    	      legend:{
    	            show:true, 
    	            placement: 'inside', 
    	            rendererOptions: {
    	                numberRows: 2
    	            }, 
    	            location:'se',
    	            marginTop: '15px',
    	            marginLeft : '25px'
    	            	
    	        }       
    	    });
    	});
	}

function createPieChart(foodDistribution) {
	debugger;
	var foodArrayDist = getJavaArrayAsAString(foodDistribution);
	$(document).ready(function(){
	var plot1 = $.jqplot('pie1', [[['vegetables',foodArrayDist[0]/70],['fruits',foodArrayDist[1]/70],['meat',foodArrayDist[2]/70],['milk',foodArrayDist[3]/70],['graminea',foodArrayDist[4]/70],['others',foodArrayDist[5]/70]]], {
        gridPadding: {top:0, bottom:38, left:0, right:0},
        seriesDefaults:{
            renderer:$.jqplot.PieRenderer, 
            trendline:{ show:true },
            rendererOptions: { padding: 8, showDataLabels: true }
        },
        legend:{
            show:true, 
            placement: 'inside', 
            rendererOptions: {
                numberRows: 3
            }, 
            location:'ne',
            marginTop: '15px'
        }       
    });
});
}

function getJavaArrayAsAString(arrayAsAString) {
    realJsString = arrayAsAString + "";
    arrayFromJava = realJsString.split(",");
    return arrayFromJava;
    }
// ####################################### ajax call to a servlet operantion ##############################################

function deleteUser(userName) {
	var xmlhttp;
	debugger;
	if(window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("userName").innerHtml=xmlhttp.responseText;
		}
	}
	xmlhttp.open("POST", "../UserServlet/?userName="+userName + "&operation=deleteUser", true);
	xmlhttp.send();
	location.reload();
}


function editUser(userName) {
	var xmlhttp;
	debugger;
	if(window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("userName").innerHtml=xmlhttp.responseText;
		}
	}
	xmlhttp.open("POST", "../UserServlet/?userName="+userName + "&operation=editUser", true);
	xmlhttp.send();
	window.location = "http://localhost:8082/procardio.web/faces/JSFfiles/UsersPage.jsp";
}

function logout(){
	var xmlhttp;
	debugger;
	if(window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open("GET","../UserServlet/?loginPageOperation=logout",true);
	xmlhttp.send();
	window.location = "http://localhost:8082/procardio.web/faces/JSFfiles/LogInPage.jsp";
}

function changeToEditUserPage(userName){
	var xmlhttp;
	debugger;
	if(window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	window.location = "http://localhost:8082/procardio.web/faces/JSFfiles/EditUsersPage.jsp";
	xmlhttp.open("POST", "../UserServlet/?userName="+userName + "&operation=changeToEditUserPage", true);
	xmlhttp.send();
	window.location = "http://localhost:8082/procardio.web/faces/JSFfiles/EditUsersPage.jsp";

}

function changeToEmployeePage(param){
	var params=param.split(",");
	var xmlhttp;
	debugger;
	if(window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	window.location = "http://localhost:8082/procardio.web/faces/JSFfiles/EmployeePage.jsp";
	xmlhttp.open("POST", "../EmployeeServlet/?pageNumber="+params[0] + "&operationType=" + params[1] + "&operation=employeePagination", true);
	xmlhttp.send();
	window.location = "http://localhost:8082/procardio.web/faces/JSFfiles/EmployeePage.jsp";
}

function selectUserToDelete(userName){
	var xmlhttp;
	debugger;
	if(window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	xmlhttp.open("POST", "../UserServlet/?userName="+userName + "&operation=selectUser", true);
	xmlhttp.send();
}

function changeToEditEmployeePage(userName){
	var xmlhttp;
	debugger;
	if(window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	window.location = "http://localhost:8082/procardio.web/faces/JSFfiles/EditEmployeePage.jsp";
	xmlhttp.open("POST", "../EmployeeServlet/?userName="+userName + "&operation=changeToEditEmployeePage", true);
	xmlhttp.send();
	window.location = "http://localhost:8082/procardio.web/faces/JSFfiles/EditEmployeePage.jsp";

}

function selectEmployeeToDelete(userName){
	var xmlhttp;
	debugger;
	if(window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	xmlhttp.open("POST", "../EmployeeServlet/?userName="+userName + "&operation=deleteEmployee", true);
	xmlhttp.send();
	window.location = "http://localhost:8082/procardio.web/faces/JSFfiles/EmployeePage.jsp";
}

function setVisibilityForParetoOptimal(){
	assert("esti pe ce trebe!")
}
// ######################################### custom checkbox style #########################################################
var checkboxHeight = "25";
var radioHeight = "25";
var selectWidth = "190";


/* No need to change anything after this */


document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; } .disabled { opacity: 0.5; filter: alpha(opacity=50); }</style>');

var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
				span[a] = document.createElement("span");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						position = "0 -" + (checkboxHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					} else {
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.clear;
				if(!inputs[a].getAttribute("disabled")) {
					span[a].onmousedown = Custom.pushed;
					span[a].onmouseup = Custom.check;
				} else {
					span[a].className = span[a].className += " disabled";
				}
			}
		}
		inputs = document.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className == "styled") {
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				if(!inputs[a].getAttribute("disabled")) {
					inputs[a].onchange = Custom.choose;
				} else {
					inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled";
				}
			}
		}
		document.onmouseup = Custom.clear;
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
		} else if(element.checked == true && element.type == "radio") {
			this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
	},
	check: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 0";
			element.checked = false;
		} else {
			if(element.type == "checkbox") {
				this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else {
				this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
				group = this.nextSibling.name;
				inputs = document.getElementsByTagName("input");
				for(a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a] != this.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
					}
				}
			}
			element.checked = true;
		}
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			}
		}
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}
window.onload = Custom.init;

function wait() {
	(function()
			{	
			  if (window.addEventListener)
			  {
			    window.addEventListener("load", hide_loading_screen, false);    
			  }
			  else
			  {
			    window.attachEvent("onload", hide_loading_screen);
			  }
			})();
}


function display_loading_screen()
{
  document.getElementById("loading_screen").style.display = 'block';
}

function hide_loading_screen()
{
  document.getElementById("loading_screen").style.display = 'none';
}